package com.chixing.day17_reflect_thread.proxy.cglibproxy;

import com.chixing.day16_io.aop_proxy.LogUtil;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 *    1.生成代理对象
 *  * 2.实现增强
 */

public class OrderProxy implements MethodInterceptor {
    private Object target;
    // 1. 生成代理对象
    public Object getProxy(Object target){
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create(); // enhancer 就是 代理对象（父类就是 目标对象）

    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object result =  method.invoke(target,args);

        if(method.getName().equals("save") || method.getName().equals("update")|| method.getName().equals("delete"))
            new LogUtil().addLog(); //执行日志记录
        return result;
    }

}

